from datetime import timedelta
from datetime import datetime
from pathlib import Path
import re
from conf import BASE_DIR


def get_absolute_path(relative_path: str, base_dir: str = None) -> str:
    # Convert the relative path to an absolute path
    absolute_path = Path(BASE_DIR) / base_dir / relative_path
    return str(absolute_path)


def get_title_and_hashtags(filename):
    """
  获取视频标题和 hashtag

  Args:
    filename: 视频文件名

  Returns:
    视频标题和 hashtag 列表
  """

    # 获取视频标题和 hashtag txt 文件名
    txt_filename = filename.replace(".mp4", ".txt")

    result = re.findall(pattern=r'(\D+|\d+.*).mp4', string=filename)
    tag_list = re.split('#', result[0])
    match = re.search(r'(\d+)(.*)', tag_list[0])
    if match:
        title_match = match.group(2)

    else:
        title_match = '关注我哦'

    # 读取 txt 文件
    with open(txt_filename, "w", encoding="utf-8") as f:
        f.write(title_match + '\n')
        for i in range(1, len(tag_list)):
            f.write("#" + tag_list[i] + ' ')
    with open(txt_filename, "r", encoding="utf-8") as f:
        content = f.read()

    # 获取标题和 hashtag
    splite_str = content.strip().split("\n")
    title = splite_str[0]
    hashtags = splite_str[1].replace("#", "").split(" ")

    # 走路摇cosplay 标签
    cosplay_tag = ['#cosplay ', '#fyp ', '#foryou ', 'game ', 'dance ', 'Anime ', 'beauty ']

    return title, cosplay_tag
    # 从文档获取标签的时候就用这么的返回值
    # return title, hashtags


def generate_schedule_time_next_day(total_videos, videos_per_day, daily_times=None, timestamps=False, start_days=0):
    """
    Generate a schedule for video uploads, starting from the next day.

    Args:
    - total_videos: Total number of videos to be uploaded.
    - videos_per_day: Number of videos to be uploaded each day.
    - daily_times: Optional list of specific times of the day to publish the videos.
    - timestamps: Boolean to decide whether to return timestamps or datetime objects.
    - start_days: Start from after start_days.

    Returns:
    - A list of scheduling times for the videos, either as timestamps or datetime objects.
    """
    if videos_per_day <= 0:
        raise ValueError("videos_per_day should be a positive integer")

    if daily_times is None:
        # Default times to publish videos if not provided
        daily_times = [6, 11, 14, 16, 22]

    if videos_per_day > len(daily_times):
        raise ValueError("videos_per_day should not exceed the length of daily_times")

    # Generate timestamps
    schedule = []
    current_time = datetime.now()

    for video in range(total_videos):
        '''
        隔天动态计算发布时间
        '''
        # day = video // videos_per_day + start_days + 1  # +1 to start from the next day
        # daily_video_index = video % videos_per_day
        #
        # # Calculate the time for the current video
        # hour = daily_times[daily_video_index]
        # time_offset = timedelta(days=day, hours=hour - current_time.hour, minutes=-current_time.minute,
        #                         seconds=-current_time.second, microseconds=-current_time.microsecond)
        # timestamp = current_time + time_offset
        '''
        当前时间晚五分钟发布
        '''
        # 创建一个timedelta对象，表示五分钟的时间差
        time_offset = timedelta(minutes=5)

        # 计算五分钟后的时间
        timestamp = current_time + time_offset

        schedule.append(timestamp)

    if timestamps:
        schedule = [int(time.timestamp()) for time in schedule]
    return schedule
